package web.adminUI;

import java.util.List;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

import web.sharedUI.ErrorMessage;
import web.sharedUI.SessionBean;
import ejb.contentManagement.PeriodMgr;
import ejb.dto.PeriodDTO;

@ManagedBean
@RequestScoped
public class PeriodBean {
	
	public PeriodBean() {
		period = new PeriodDTO();
		setErrorMessage(null);
	}
	
	@EJB
	private PeriodMgr periodMgr; 
	
	@SuppressWarnings("unused")
	private List<PeriodDTO> allPeriods;

	private int periodId;
	private PeriodDTO period;
	private String errorMessage;
	
	/**
	 * Delete Period from database
	 * @param id
	 * @return redirect to page <code>managePeriods</code>
	 */
	public String deletePeriod(int id) {
		boolean result = periodMgr.deletePeriod(id);
		if(!result){
			SessionBean.displayError(ErrorMessage._DELETE);
			return null;
		}
		return "/admins/managePeriods?faces-redirect=true";
	}
	
	/**
	 * Update Period in database
	 * @return redirect to page <code>managePeriods</code>
	 */
	public String updatePeriod() {
		boolean result = periodMgr.updatePeriod(period);	
		if(!result){
			SessionBean.displayError(ErrorMessage._UPDATE);
			return null;
		}
		return "/admins/managePeriods?faces-redirect=true";
	}
	
	/**
	 * Insert Period in database
	 * @return redirect to page <code>managePeriods</code>
	 */
	public String insertPeriod() {
		boolean result = periodMgr.insertPeriod(period);
		if(!result){
			SessionBean.displayError(ErrorMessage._INSERT);
			return null;
		}
		return "/admins/managePeriods?faces-redirect=true";
	}
	
	/*
	 * GETTERS & SETTERS
	 */
	
	public List<PeriodDTO> getAllPeriods() {
		return periodMgr.getAllPeriodsDTO();
	}

	public void setAllPeriods(List<PeriodDTO> allPeriods) {
		this.allPeriods = allPeriods;
	}


	public int getPeriodId() {
		return periodId;
	}

	public void setPeriodId(int periodId) {
		this.periodId = periodId;
		period = periodMgr.getPeriodDTO(periodId);
		if(period == null)
			SessionBean.notFound();
	}

	public PeriodDTO getPeriod() {
		return period;
	}

	public void setPeriod(PeriodDTO period) {
		this.period = period;
	}

	public String getErrorMessage() {
		return errorMessage;
	}

	public void setErrorMessage(String errorMessage) {
		this.errorMessage = errorMessage;
	}
	

	
}
